package com.nexgo.payment.fragment.left;

import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;

import com.amap.api.location.AMapLocation;
import com.nexgo.payment.App;
import com.nexgo.payment.R;
import com.nexgo.payment.aop.annotation.RunOnMainThread;
import com.nexgo.payment.api.ApiLocationService;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.databases.PasswordInfo;
import com.nexgo.payment.util.EncryptUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * 左侧菜单操作
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年11月29日
 */
public class LeftPresenter implements LeftContract.Presenter, ApiLocationService.LocationInterface {
    private Logger mLog = LoggerFactory.getLogger(LeftPresenter.class.getSimpleName());

    private LeftContract.View mView;
    private DataSource mRepository;
    private ApiLocationService mLocationService;

    public LeftPresenter(LeftContract.View view, DataSource repository) {
        mView = checkNotNull(view);
        mRepository = checkNotNull(repository);
        view.setPresenter(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        if (mLocationService != null) {
            mLocationService.destroyLocation();
        }
    }

    @Override
    public void startLocation() {
        if (mLocationService == null) {
            mLocationService = new ApiLocationService(App.sContext, this);
        }
        mLocationService.startLocation();
    }

    @Override
    public void checkUpdate(FragmentActivity activity) {
        postMainThread(() -> mView.showIsNewestVersion());
        // FIXME: 2017/12/6 需要用到的升级功能的，做相应修改即可使用
        /*new UpdateHelper(activity)
                .setCheckUrl("http://jcodecraeer.com/update.php")
                .setIsAutoInstall(true)
                .setCheckListener(new OnCheckListener() {

                    @Override
                    public void onStart() {
                        postMainThread(() -> mView.setCheckUpdateIndicator(true));
                    }

                    @Override
                    public void onResult(boolean hasNewVersion) {
                        postMainThread(() -> mView.setCheckUpdateIndicator(false));
                        if (!hasNewVersion){
                            postMainThread(() -> mView.showIsNewestVersion());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        postMainThread(() -> mView.setCheckUpdateIndicator(false));
                    }
                })
                .build();*/
    }

    @Override
    public void login2Setting(String password) {
        if (TextUtils.isEmpty(password)) {
            postMainThread(() -> mView.showEmpty());
            return;
        }
        PasswordInfo info = mRepository.getPasswordInfo();
        if (info == null || TextUtils.isEmpty(info.getSecurityPassword())) {
            postMainThread(() -> mView.showErr(App.sContext.getString(R.string.password_is_not_exist_plz_init_again)));
            return;
        }
        if (!info.getSecurityPassword().equals(EncryptUtils.encryptSHA256ToString(password))) {
            postMainThread(() -> mView.showErr(App.sContext.getString(R.string.security_password_error)));
            return;
        }
        postMainThread(() -> mView.intent2Setting());
    }

    @Override
    public void onLocation(AMapLocation location) {
        postMainThread(() -> mView.showLocation(location));
    }

    @RunOnMainThread
    private void postMainThread(Runnable runnable) {
        if (mView.isActive()) {
            runnable.run();
        }
    }
}
